package task

import (
	"go-test/src/com/miao/server/context"
)

func HandleConnectedTask(channelContext *context.ChannelContext){
	groupContext := channelContext.GroupContext
	listener := groupContext.ServerListener
	if listener!=nil {
		//连接监听
		listener.AfterConnected(channelContext)
	}
}

func HandleCloseTask(channelContext *context.ChannelContext)  {
	groupContext := channelContext.GroupContext
	listener := groupContext.ServerListener
	if listener!=nil {
		//关闭前监听
		listener.BeforeClose(channelContext)
	}
	unbindWithClose(groupContext,channelContext)
}


func unbindWithClose(groupContext *context.GroupContext,channelContext *context.ChannelContext)  {
	//解除Ids
	context.UnbindAll(groupContext.Ids,channelContext)
	//解除users
	context.UnbindAll(groupContext.Users,channelContext)
	//解除groups
	context.UnbindAll(groupContext.Groups,channelContext)
	groupContext.Connections.Remove(channelContext)
	defer func() {
		close(channelContext.ReadChan)
		close(channelContext.SendChan)
	}()
}